package cn.demo.controller;

import cn.demo.Util.Page;
import cn.demo.Util.Result;
import cn.demo.entity.Product;
import cn.demo.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author weizhongli
 * @Date 2020/4/5
 * @Version v1.0
 * @Description 商品管理控制
 **/
@Controller
@RequestMapping("product")
public class ProductController {

    //依赖注入service
    @Autowired
    private ProductService productService;

    /*
     * 查询所有商品
     * */
    @PreAuthorize("hasAuthority('Product_select')") //权限控制
    @RequestMapping("findAll")
    @ResponseBody
    public Result findAll(@RequestParam(defaultValue = "1") int PageIndex, @RequestParam(defaultValue = "2") int PageSize, String selectProductName, Integer selectProvider) {
        try {
            if (selectProvider == null) {
                selectProvider = 0;
            }
            Page page = productService.findAll(PageIndex, PageSize, selectProductName, selectProvider);
            return new Result(true, "查询成功", page);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败");
        }

    }


    /*
    * 根据id查询商品
    * */
    @PreAuthorize("hasAuthority('Product_select')") //权限控制
    @RequestMapping("findById")
    public String findById(Integer id, HttpServletRequest request) {
        Product product = productService.findById(id);
        request.setAttribute("product", product);
        return "productUpdate";
    }

    /*
    * 根据名字查询商品
    * */
    @PreAuthorize("hasAuthority('Product_select')") //权限控制
    @RequestMapping("findByName")
    public Result findByName(String name) {
        try {
            Product product = productService.findByName(name);
            return new Result(true, "查询成功", product);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败");
        }
    }

    /*
    * 修改商品
    * */
    @PreAuthorize("hasAuthority('Product_edit')") //权限控制
    @RequestMapping("updateProduct")
    @ResponseBody
    public Result updateProduct(Product product) {
        try {
            int i = productService.updateProduct(product);
            return new Result(true, "修改成功", i);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "修改失败");
        }
    }

    /*
    * 添加商品
    * */
    @PreAuthorize("hasAuthority('Product_add')") //权限控制
    @RequestMapping("addProduct")
    @ResponseBody
    public Result addProduct(Product product) {
        try {
            productService.addProduct(product);
            return new Result(true, "添加成功","1");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加失败","0");
        }
    }

    /*
    * 根据id删除商品
    * */
    @PreAuthorize("hasAuthority('Product_delete')") //权限控制
    @RequestMapping("deleteProduct")
    @ResponseBody
    public Result deleteProduct(String str) {
        try {
            String[] ids = str.split(",");
            for (int i = 0; i < ids.length; i++) {
                productService.deleteProduct(Integer.parseInt(ids[i].trim()));
            }
            return new Result(true, "删除成功", "true");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除失败", "false");
        }
    }

    /*
     * 导出数据
     * */
    @RequestMapping("/daochu")
    public String download(Model model) throws Exception {
        List<Product> list = productService.selectProductList();
       /* for (Product item : list) {
            Date birthday = item.getCreationDate();
            SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd");
            String format = myFormat.format(birthday);
            item.setCtratedate(format);
        }*/
        model.addAttribute("list", list);
        return "productEx";
    }

    /**
     * 数据回显
     */
    @RequestMapping("/showProductList")
    public String showBillList() {
        return "productList";
    }

    /*
     * 数据展示
     * */
    @PreAuthorize("hasAuthority('Product_show')") //权限控制
    @ResponseBody
    @RequestMapping("/productechart")
    public Result selectbill() {
        try {
            List<Product> selectProductList = productService.selectProductList();
            return new Result(true, "展示成功", selectProductList);
        } catch (Exception e) {
            return new Result(false, "展示失败");
        }
    }
}